package com.lln.cjcx.controller;
import com.lln.cjcx.pojo.Message;
import com.lln.cjcx.pojo.MyOkHttpClient;
import com.lln.cjcx.pojo.SearchCondition;
import com.lln.cjcx.pojo.Term;
import com.lln.cjcx.pojo.grade.TermGrade;
import com.lln.cjcx.service.GradeExportService;
import com.lln.cjcx.service.GradeSearchService;
import com.lln.cjcx.value.CommonValue;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import static java.util.logging.Level.INFO;
import java.util.logging.Logger;

/**
 * @author LiLinnan
 * @version 1.0
 * @date 2020/1/22 20:43
 */
@Controller
@RequestMapping("/grade")
public final class GradeSearchController {
	private final static String EXPORT_COL = "0";
	private final static String EXPORT_ROW = "1";
	private final GradeSearchService gradeSearchService;
	private final GradeExportService gradeExportService;
	@Autowired
	public GradeSearchController(@NotNull GradeSearchService gradeSearchService,@NotNull GradeExportService gradeExportService) {
		super();
		String str = "【信息】正在初始化成绩搜索控制器...";
		System.out.println(str);
		Logger.getGlobal().log(INFO,str);
		Intrinsics.checkParameterIsNotNull(gradeSearchService,"gradeSearchService");
		Intrinsics.checkParameterIsNotNull(gradeExportService,"gradeExportService");
		this.gradeSearchService = gradeSearchService;
		this.gradeExportService = gradeExportService;
	}
	@NotNull
	@Contract("_ -> new")
	@ResponseBody
	@RequestMapping("/terms_get")
	public Message termsGet(@NotNull HttpSession session) {
		kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull(session,"session");
		MyOkHttpClient myOkHttpClient = (MyOkHttpClient) session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION);
		kotlin.jvm.internal.Intrinsics.checkNotNull(myOkHttpClient,"myOkHttpclient should not be null");
		List<Term> terms = gradeSearchService.searchTerm(myOkHttpClient);
		if (terms == null) {
			session.invalidate();
			return new Message(-2, "登录状态掉了");
		}
		return new Message(1, terms);
	}
	@ResponseBody
	@RequestMapping("/current_term")
	public Message currentTermSearch(HttpSession session) {
		MyOkHttpClient myOkHttpClient = (MyOkHttpClient) session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION);
		List<TermGrade> grades = gradeSearchService.searchCurrentTermGrade(myOkHttpClient);
		if (grades == null) {
			session.invalidate();
			return new Message(-2, "登录状态掉了");
		}
		myOkHttpClient.grades = grades;
		return new Message(1, grades);
	}
	@ResponseBody
	@RequestMapping("/all_term")
	public Message allTermSearch(SearchCondition searchCondition, HttpSession session) {
		MyOkHttpClient myOkHttpClient = (MyOkHttpClient) session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION);
		List<TermGrade> grades = gradeSearchService.searchAllTermGrade(myOkHttpClient, searchCondition);
		if (grades == null) {
			session.invalidate();
			return new Message(-2, "登录状态掉了");
		}
		myOkHttpClient.grades = grades;
		return new Message(1, grades);
	}
	@ResponseBody
	@RequestMapping("/export")
	public Message exportResult(HttpServletRequest request, HttpSession session, @RequestParam(defaultValue = EXPORT_COL) String type) {
		MyOkHttpClient myOkHttpClient = (MyOkHttpClient) session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION);
		List<TermGrade> grades = myOkHttpClient.grades;
		if (grades == null) {
			return new Message(-1, "还没有查到任何结果");
		}
		String path = request.getServletContext().getRealPath("/xlsx/");
		String result;
		if (EXPORT_COL.equals(type)) {
			result = gradeExportService.gradeExportCol(grades, path, myOkHttpClient.student);
		} else if (EXPORT_ROW.equals(type)) {
			result = gradeExportService.gradeExportRow(grades, path, myOkHttpClient.student);
		} else {
			return new Message(-1, "导出类型错误");
		}
		return new Message(result == null ? -1 : 1, result == null ? "fail" : result);
	}
}
